﻿using UnityEngine;
using System.Collections;
using GDGeek;

public class Ctrl : MonoBehaviour {

    private FSM fsm_ = new FSM();
    public void fsmPost(string msg)
    {
        fsm_.post(msg);
    }
    public View _view = null;

    void Start()
    {
        fsm_.addState("begin", beginState());
        fsm_.addState("play", playState());
        fsm_.addState("end", endState());
        fsm_.init("begin");
    }

    private State endState()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {
            _view.end.gameObject.SetActive(true);
        };
        state.onOver += delegate
        {
            _view.end.gameObject.SetActive(false);
        };
        state.addEvent("end", "begin");
        return state;
    }

    private State playState()
    {
        StateWithEventMap state = TaskState.Create(delegate
        {
            TaskWait tw = new TaskWait();
            tw.setAllTime(3f);
            return tw;
        }, fsm_, "end");
        state.onStart += delegate
        {
            _view.play.gameObject.SetActive(true);
        };
        state.onOver += delegate
        {
            _view.play.gameObject.SetActive(false);
        };
        return state;
    }

    private State beginState()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate {
            _view.begin.gameObject.SetActive(true);
        };
        state.onOver += delegate
        {
            _view.begin.gameObject.SetActive(false);
        };
        state.addEvent("begin", "play");
        return state;
    }

    void Update()
    {

    }
}
